package com.centling.smartSealForPhone.activity

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.View
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.adapter.AuditorInfoAuthorityAdapter
import com.centling.smartSealForPhone.constants.SPConstants
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.PhoneNumCheck
import com.centling.smartSealForPhone.utils.SPUtil
import com.centling.smartSealForPhone.utils.ShowToast
import kotlinx.android.synthetic.main.activity_auditor_info.*
import org.json.JSONException
import org.json.JSONObject
import java.util.*

/**
 * Created by Jessie on 2016/8/26.
 */
class AuditorInfoActivity : TitleBarActivity() {
    var isEdit: Boolean = false
    var linearLayoutManager: LinearLayoutManager? = null
    var adapter: AuditorInfoAuthorityAdapter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleBarText("个人信息")
        setContentView(R.layout.activity_auditor_info)
        tv_auditor_device_list.setOnClickListener { startActivity(Intent(mContext, AuditorDeviceActivity::class.java)) }

        tv_auditor_info_username.text = SPUtil.getString(SPConstants.USER_NAME)
        tv_auditor_info_realname.text = SPUtil.getString(SPConstants.REAL_NAME)
        tv_auditor_info_phone.text = SPUtil.getString(SPConstants.PHONE)
        linearLayoutManager = LinearLayoutManager(mContext)
        adapter = AuditorInfoAuthorityAdapter(mContext, SPUtil.getStrListValue(mContext, SPConstants.AUTHORITY))
        rv_auditor_authority.layoutManager = linearLayoutManager
        rv_auditor_authority.adapter = adapter
        tv_auditor_info_role.text = SPUtil.getString(SPConstants.ROLENAME);
        sw_auditor_info_isReceiveMessage.isEnabled = false
        sw_auditor_info_isReceiveMessage.isOn = SPUtil.getString(SPConstants.RECEIVE_MESSAGE) == "1"
        tv_auditor_info_reset.setOnClickListener {
            if (isEdit) {
                sendRequest()
            } else {
                tv_auditor_info_reset.text = "确定"
                et_auditor_info_realname.visibility = View.VISIBLE
                et_auditor_info_realname.setText(tv_auditor_info_realname.text)
//                et_auditor_info_phone.visibility = View.VISIBLE
//                et_auditor_info_phone.setText(tv_auditor_info_phone.text)
                sw_auditor_info_isReceiveMessage.isEnabled = true
                tv_auditor_info_phone.setOnClickListener {
                    startActivityForResult(Intent(mContext, PhoneNumberCheckActivity::class.java), 1)
                }
                tv_auditor_info_realname.visibility = View.GONE
//                tv_auditor_info_phone.visibility = View.GONE
                isEdit = true
                tv_auditor_info_phone.isClickable = true
            }
        }

        tv_auditor_info_delete.setOnClickListener { finish() }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode ==1 && resultCode == 1001) {
            tv_auditor_info_phone.text = data!!.getStringExtra("phoneNumber")
        }
    }

    fun sendRequest() {
        val id = SPUtil.getString(SPConstants.USER_ID)
        val param = HashMap<String, String>()
        param.put("userId", id)
        param.put("userName", "")
        param.put("realName", et_auditor_info_realname.text.toString())
        param.put("phone", tv_auditor_info_phone.text.toString())
        param.put("department", "")
        param.put("avatar", "")
        param.put("receiveMsg", if(sw_auditor_info_isReceiveMessage.isOn) "1" else "0")
        if (!PhoneNumCheck.isMobileNo(tv_auditor_info_phone.text.toString())) {
            ShowToast.shortTime("请输入正确的手机号")
            return
        }
        if (TextUtils.isEmpty(et_auditor_info_realname.text.toString())) {
            ShowToast.shortTime("请输入真实姓名")
            return
        }
        putJsonEnqueue(UrlConstants.EDIT_USERINFO, param, object : OkHttpUtil.NetCallBack {
            override fun onSucceed(json: String) {
                ShowToast.shortTime("修改个人信息成功")
                tv_auditor_info_reset.text = "编辑信息"
                tv_auditor_info_realname.visibility = View.VISIBLE
                tv_auditor_info_realname.text = et_auditor_info_realname.text
                et_auditor_info_realname.visibility = View.GONE
//                tv_auditor_info_phone.visibility = View.VISIBLE
//                tv_auditor_info_phone.text = et_auditor_info_phone.text
                et_auditor_info_phone.visibility = View.GONE
                isEdit = false
                tv_auditor_info_phone.isClickable = false
                sw_auditor_info_isReceiveMessage.isEnabled = false

                var data: JSONObject? = null
                try {
                    data = JSONObject(json)
                } catch (e: JSONException) {
                    e.printStackTrace()
                }
                SPUtil.setString(SPConstants.PHONE, data!!.optString("phone"))
                SPUtil.setString(SPConstants.REAL_NAME, data.optString("realName"))
                SPUtil.setString(SPConstants.RECEIVE_MESSAGE, data.optString("receiveMsg"))
            }

            override fun onFailed(reason: String) {
                ShowToast.shortTime(reason)
                tv_auditor_info_phone.isClickable = false
                sw_auditor_info_isReceiveMessage.isEnabled = false
            }
        })
    }
}

